﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Economo.Services.DataContext
{
    public class AccountsDataContext : IDisposable 
    {
        DataClassesDataContext _context = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["Economo"].ConnectionString);

        public Account GetAccountById(Int16 id)
        {
            try
            {
                Account account = (from b in _context.Accounts
                                           where b.Id == id
                                           select b).FirstOrDefault();
                return account;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting account by id.", ex));
                return null;
            }
        }

        public Int16 InsertAccount(Account newAccount)
        {
            try
            {

                InsertAccountResult result = _context.InsertAccount(newAccount.Description,newAccount.AccountTypeId,newAccount.BankId,newAccount.BankAccountId,newAccount.CreditCardId).FirstOrDefault<InsertAccountResult>();
                if (result.Id.HasValue)
                    return result.Id.Value;
                else
                    throw new ExceptionHandling.DataAccessException("Impossible to retrieve autogenerated id when inserting new row.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error inserting new account.", ex));
                return 0;
            }
        }

        public void UpdateAccount(Account account)
        {
            try
            {
                Account updated = (from b in _context.Accounts
                                       where b.Id == account.Id
                                       select b).First();
                if (updated != null)
                {
                    updated.Description = account.Description;
                    updated.AccountTypeId = account.AccountTypeId;
                    updated.BankId = account.BankId;
                    updated.BankAccountId = account.BankAccountId;
                    updated.CreditCardId = account.CreditCardId;
                    _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                }
                else
                    throw new ExceptionHandling.DataAccessException("Account not found with Id=" + account.Id.ToString());
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error updating account.", ex));
            }
        }
        public void DeleteAccount(Account account)
        {
            try
            {
                _context.Accounts.DeleteOnSubmit(account);
                _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting account.", ex));
            }
        }
        public void DeleteAccount(byte id)
        {
            try
            {
                Account account = GetAccountById(id);
                if (account != null)
                    DeleteAccount(account);
                else
                    throw new ExceptionHandling.DataAccessException("Account to delete not found.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting account.", ex));
            }
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
